package net.kleinhenz.norabase.client;

import java.util.Date;
import java.util.LinkedHashMap;

import net.kleinhenz.norabase.shared.model.Battlegroup;
import net.kleinhenz.norabase.shared.model.BattlegroupComment;
import net.kleinhenz.norabase.shared.model.Comment;
import net.kleinhenz.norabase.shared.model.Rune;
import net.kleinhenz.norabase.shared.model.RuneComment;

import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.types.Alignment;

public class RatingCanvas extends VLayout {

	LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();  

	private Battlegroup<Rune> battlegroup = null;
	private Rune rune = null;
	
	private boolean writable = false;
	
	private RatingList ratingList = null;
	private TextAreaItem commentField = null;
	private SelectItem ratingField = null;
	
	public RatingCanvas() {
		this(false);
	}
	
	public RatingCanvas(boolean writable) {
		this.writable = writable;
		
		valueMap.put("1", "<b><span style='color:#000000'>&diams;</span><span style='color:#bbbbbb'>&diams;&diams;&diams;&diams;</span> - Bad</b>");  
		valueMap.put("2", "<b><span style='color:#000000'>&diams;&diams;</span><span style='color:#bbbbbb'>&diams;&diams;&diams;</span> - Not good enough</b>");  
        valueMap.put("3", "<b><span style='color:#000000'>&diams;&diams;&diams;</span><span style='color:#bbbbbb'>&diams;&diams;</span> - Usable</b>");  
        valueMap.put("4", "<b><b><span style='color:#000000'>&diams;&diams;&diams;&diams;</span><span style='color:#bbbbbb'>&diams;</span> - Good</b>");  
        valueMap.put("5", "<b><b><span style='color:#000000'>&diams;&diams;&diams;&diams;&diams;</span><span style='color:#bbbbbb'></span> - Don't miss it!</b>");  
		
		init();
	}
	
	public void init() {
		
		this.setMembersMargin(10);
		ratingList = new RatingList();
		addMember(ratingList);

		if (writable) {
			final DynamicForm dynamicForm = new DynamicForm();
			commentField = new TextAreaItem("comment", "Comment");
			commentField.setWidth("100%");
			commentField.setValidators(new NullValidator());
	
			ratingField = new SelectItem();  
			ratingField.setWidth("100%");
			ratingField.setTitle("Rating");  
			ratingField.setValueMap(valueMap);  
			ratingField.setValidators(new NullValidator());
			
			dynamicForm.setFields(new FormItem[] { commentField, ratingField});
			addMember(dynamicForm);
			
			HLayout hLayout = new HLayout(10);
			hLayout.setAlign(Alignment.RIGHT);
			hLayout.setDefaultLayoutAlign(VerticalAlignment.CENTER);
			
			Button btnSave = new Button("Save");
			btnSave.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (dynamicForm.validate()) {
						Comment c = null;
						if (battlegroup!=null) {
							c = new BattlegroupComment();
							((BattlegroupComment)c).setBattlegroupId(battlegroup.getId());
						} else if (rune!=null) {
							c = new RuneComment();
							((RuneComment)c).setRuneId(rune.getId());
							((RuneComment)c).setType(Rune.typeToString(rune));					
						}
						c.setAccountId(NoraBaseEditor.getLoginInfo().getAccount().getEmail());
						c.setDate(new Date());
						c.setComment((String)commentField.getValue());				
						c.setRating(Integer.parseInt((String)ratingField.getValue()));
						addRating(c);
					}
				}
			});
			hLayout.addMember(btnSave);
			addMember(hLayout);
		}
	}

	public void addRating(Comment entry) {
		ratingList.addRating(entry);
	}
	
	public void clear() {
		ratingList.clear();
	}

	public void loadRune(Rune rune) {
		this.rune = rune;
		ratingList.loadRune(rune);
	}

	public void loadBattlegroup(Battlegroup<Rune> battlegroup) {
		this.battlegroup = battlegroup;
		ratingList.loadBattlegroup(battlegroup);
	}
}
